
package com.yunmai.library.util;

import android.annotation.SuppressLint;
import android.util.Log;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@SuppressLint("SimpleDateFormat")
public final class DateHelper {

  /**
   * 获取当前时间
   */
  public static Date getCurrentDate() {
    return new Date();
  }

  /**
   * 获取默认当前时间
   */
  public static Date defualtDateTime() {
    return StrToDate("2001-01-01 00:00:01");
  }

  /**
   * 获取当前时间：yyyy
   */
  public static int getCurrentYearNum() {
    return Integer.valueOf(new SimpleDateFormat(EnumDateFormatter.DATE_YEAR_NUM.getFormatter())
        .format(new Date()));
  }

  /**
   * 判断是否润年 润年 2月份 29天 平年2月份28天
   */
  public static boolean isLeapYear(int year) {
    return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
  }

  /**
   * 获取当前时间：yyyy-MM-dd HH:mm:ss
   */
  public static String getCurrentDateTimeString() {
    return new SimpleDateFormat(EnumDateFormatter.DATE_TIME_STR.getFormatter())
        .format(new Date());
  }

  /**
   * 数据转换
   */
  public static int parseDateNum(Date date, EnumDateFormatter dataFormatter) {
    String formatterString =
        dataFormatter.getFormatter().replace("-",
            "").replace(":",
            "").replace(" ",
            "");
    String value = new SimpleDateFormat(formatterString).format(date);
    return Integer.valueOf(value);
  }

  /**
   * 数据转换成 yyyy
   */
  public static int dateToNumYear(Date date) {
    return parseDateNum(date, EnumDateFormatter.DATE_YEAR_NUM);
  }

  /**
   * 数据转换成MM
   */
  public static short dateToNumMonth(Date date) {
    return Short.valueOf(String.valueOf(parseDateNum(date, EnumDateFormatter.DATE_MOTH_NUM)));
  }

  /**
   * 获取当前时间：yyyy-MM-dd HH:mm:ss
   */
  public static String getCurrentYear() {
    return new SimpleDateFormat(EnumDateFormatter.DATE_NUM_2.getFormatter()).format(new Date());
  }

  /**
   * 获取当前时间：yyyyMMddHHmmss
   */
  public static String getCurrentDateTimeNum() {
    return new SimpleDateFormat(EnumDateFormatter.DATE_TIME_NUM.getFormatter())
        .format(new Date());
  }

  /**
   * 获取当前时间：yyyy-MM-dd HH:mm:ss:SS
   */
  public static String getCurrentDateTimeLongString() {
    return new SimpleDateFormat(EnumDateFormatter.DATE_TIME_LONG_STR.getFormatter())
        .format(new Date());
  }

  /**
   * 获取当前时间：yyyyMMddHHmmssSS
   */
  public static String getCurrentDateTimeLongNum() {
    return new SimpleDateFormat(EnumDateFormatter.DATE_TIME_LONG_NUM.getFormatter())
        .format(new Date());
  }

  /**
   * 获取当前时间：yyyy-MM-dd
   */
  public static String getCurrentDateString() {
    return new SimpleDateFormat(EnumDateFormatter.DATE_STR.getFormatter()).format(new Date());
  }

  /**
   * 获取当前时间：yyyyMMdd
   */
  public static String getCurrentDateNumStr() {
    return new SimpleDateFormat(EnumDateFormatter.DATE_NUM.getFormatter()).format(new Date());
  }

  public static String getCurrentMonthDateStr(Date date) {
    if (date == null) date = new Date();
    return new SimpleDateFormat(EnumDateFormatter.DATE_MONTH_NUM.getFormatter())
        .format(date);
  }

  /**
   * 获取当前时间：yyyyMMdd
   */
  public static int getCurrentDateNum() {
    return Integer.valueOf(new SimpleDateFormat(EnumDateFormatter.DATE_NUM.getFormatter())
        .format(new Date()));
  }

  /**
   * 获取当前时间：yyyyMMdd
   */
  public static int getDateNum(Date date) {
    return Integer.valueOf(
        new SimpleDateFormat(EnumDateFormatter.DATE_NUM.getFormatter()).format(date));
  }

  /**
   * 获取当前时间：HH:mm:ss
   */
  public static String getCurrentDateTimeHourString() {
    return new SimpleDateFormat(EnumDateFormatter.DATE_TIME_HOUR_STR.getFormatter())
        .format(new Date());
  }

  /**
   * 获取当前时间：HHmmss
   */
  public static String getCurrentDateTimeHourNum() {
    return new SimpleDateFormat(EnumDateFormatter.DATE_TIME_HOUR_NUM.getFormatter())
        .format(new Date());
  }

  /**
   * 时间转化：Date to yyyyMMdd
   */
  public static String parseDateyymmdd(Date date) {
    return new SimpleDateFormat(EnumDateFormatter.DATE_NUM.getFormatter()).format(date);
  }

  /**
   * 时间转化：Date to yyyy-MM-dd
   */
  public static String parseDateToyymmdd(Date date) {
    return new SimpleDateFormat(EnumDateFormatter.DATE_STR.getFormatter()).format(date);
  }

  /**
   * 时间转化：Date to MMddHH
   */
  public static String parseDateMMddHH() {
    return new SimpleDateFormat("MMddHH").format(new Date());
  }

  /**
   * 时间转化：Date to yyyyMMdd
   */
  public static String parseDate(Date date) {
    return new SimpleDateFormat(EnumDateFormatter.DATE_STR.getFormatter()).format(date);
  }

  /**
   * 时间转化：Date to yyyy-MM-dd-HH
   */
  public static String parseDateHour(Date date) {
    return new SimpleDateFormat(EnumDateFormatter.DATE_HOUR_STR.getFormatter()).format(date);
  }

  /**
   * 时间转化：Date to yyyy-MM-dd HH:mm:ss
   */
  public static String parseDateTime(Date date) {
    return new SimpleDateFormat(EnumDateFormatter.DATE_TIME_STR.getFormatter()).format(date);
  }

  /**
   * 时间转化：Date to formatter string
   */
  public static String parseDateByFormatter(Date date, EnumDateFormatter formatter) {
    return new SimpleDateFormat(formatter.getFormatter()).format(date);
  }

  /**
   * 字符串转换成日期
   *
   * @return date
   */
  public static Date StrToDate(String str, EnumDateFormatter formatter) {
    SimpleDateFormat format = new SimpleDateFormat(formatter.getFormatter());
    Date date = null;
    try {
      date = format.parse(str);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }

  /**
   * 字符串转换成日期
   *
   * @return date
   */
  public static Date StrToDate(String str) {
    if (str == null || str == "") {
      return null;
    }
    SimpleDateFormat format = new SimpleDateFormat(EnumDateFormatter.DATE_STR.getFormatter());
    Date date = null;
    try {
      date = format.parse(str);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }

  /**
   * 字符串转换成时间
   *
   * @param str yyyy-MM-dd HH:mm:ss
   * @return date
   */
  public static Date StrToDateTime(String str) {
    SimpleDateFormat format =
        new SimpleDateFormat(EnumDateFormatter.DATE_TIME_STR.getFormatter());
    Date date = null;
    try {
      date = format.parse(str);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }

  /**
   * 计算月份的第一天和最后一天 用于报表查询
   */
  public static Date[] getMonth(int month) {
    Date[] monthDay = new Date[2];
    try {
      // 获取当前月的第一天
      Calendar cal = Calendar.getInstance(); // 获取当前日期
      cal.add(Calendar.MONTH, month);
      cal.set(Calendar.DAY_OF_MONTH, 1); // 设置为1号，当前日期即为本月第一天
      monthDay[0] = cal.getTime();
      // 获取当前月的最后一天
      cal.add(Calendar.MONTH, 1);
      cal.set(Calendar.DAY_OF_MONTH, 0); // 设置为1号，当前日期既为本月第一天
      monthDay[1] = cal.getTime();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return monthDay;
  }

  /**
   * @param strDate:yyyy-MM-dd
   * @return
   */
  public static Date parseDate(String strDate) {
    return parseDate(strDate, EnumDateFormatter.DATE_STR.getFormatter());
  }

  /**
   * @param strDate:yyyy-MM-dd HH:mm:ss
   */
  public static Date parseDateTime(String strDate) {
    return parseDate(strDate, EnumDateFormatter.DATE_TIME_STR.getFormatter());
  }

  /**
   * parseDate
   */
  public static Date parseDate(String strDate, String pattern) {
    Date date = null;
    String YYYYMMDD = EnumDateFormatter.DATE_TIME_STR.getFormatter();
    try {
      if (pattern == null) {
        pattern = YYYYMMDD;
      }
      SimpleDateFormat format = new SimpleDateFormat(pattern);
      date = format.parse(strDate);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return date;
  }

  /**
   * format date
   */
  public static String formatDate(Date date) {
    return formatDate(date, null);
  }

  /**
   * format date
   */
  public static String formatDate(Date date, String pattern) {
    String strDate = null;
    String YYYYMMDD = "yyyy-MM-dd";
    try {
      if (pattern == null) {
        pattern = YYYYMMDD;
      }
      SimpleDateFormat format = new SimpleDateFormat(pattern);
      strDate = format.format(date);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return strDate;
  }

  /**
   * 将Date 类型转换为 yyyy-MM-dd HH:mm:ss格式字符串
   *
   * @param pattern yyyy-MM-dd HH:mm:ss
   * @return String
   */
  public static String dataToString(Date date, String pattern) {
    String strDate = null;

    String YYYYMMDD = "yyyy-MM-dd HH:mm:ss"; //yyyy-MM-dd HH:mm:ss
    try {
      if (pattern == null) {
        pattern = YYYYMMDD;
      }
      SimpleDateFormat format = new SimpleDateFormat(pattern);
      strDate = format.format(date);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return strDate;
  }

  /**
   * 取得一年的第几周
   */
  public static int getWeekOfYear(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    int week_of_year = c.get(Calendar.WEEK_OF_YEAR);
    return week_of_year - 1;
  }

  /**
   * 根据日期取得对应周周日日期
   */
  public static Date getSunOfWeek(Date date) {
    int FIRST_DAY_OF_WEEK = Calendar.SUNDAY; // 周日是一周的第一天
    Calendar sunday = Calendar.getInstance();
    sunday.setTime(date);
    sunday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);
    sunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    return sunday.getTime();
  }

  /**
   * 根据日期取得对应周周六日期
   */
  public static Date getSaturdayOfWeek(Date date) {
    int FIRST_DAY_OF_WEEK = Calendar.SUNDAY; // 周日是一周的第一天
    Calendar saturday = Calendar.getInstance();
    saturday.setTime(date);
    saturday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);
    saturday.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
    return saturday.getTime();
  }

  /**
   * 取得月最后一天
   */
  public static Date getLastDateOfMonth(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
    return c.getTime();
  }

  /**
   * 数据转换�?
   */
  public static short dateToNumQuarter(Date date) {
    int moth = parseDateNum(date, EnumDateFormatter.DATE_MOTH_NUM);
    short value = 0;
    switch (moth) {
      case 1:
      case 2:
      case 3:
        value = 1;
        break;
      case 4:
      case 5:
      case 6:
        value = 2;
        break;
      case 7:
      case 8:
      case 9:
        value = 3;
        break;
      case 10:
      case 11:
      case 12:
        value = 4;
        break;
      default:
        break;
    }
    return value;
  }

  public static short dateToNumWeek(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int week = cal.get(Calendar.DAY_OF_WEEK);
    week = week - 1;
    if (week == 0) {
      week = 7;
    }
    return Short.valueOf(String.valueOf(week));
  }

  public static short dateToNumWeekSun(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int week = cal.get(Calendar.DAY_OF_WEEK);
    week = week - 1;
    return Short.valueOf(String.valueOf(week));
  }

  public static Date getOffsetDay(Date date, int offset) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.DAY_OF_MONTH, offset);
    date = calendar.getTime();
    return date;
  }

  /**
   * 根据Unix时间戳，转换成Date
   **/
  public static Date timeStampToDate(Integer timestampString) {
    Long timestamp = timestampString * 1000L;
    return new Date(timestamp);
  }

  /**
   * 时间字符串： yyyy-MM-dd HH:mm:ss 转换成 截止时间
   *
   * @return int
   */
  public static int dateToTimeStamp(String dateStr) {
    Date time = StrToDate(dateStr, EnumDateFormatter.DATE_TIME_STR);
    float timeTamp = time.getTime() / 1000f;
    return DataUtil.toInt(timeTamp);
  }

  /**
   * 获取当前时间 Unix截止时间：  new Date().getTime() / 1000f;
   *
   * @return int
   */
  public static int getCurrentUnixTimeStamp() {
    float timeTamp = getCurrentDate().getTime() / 1000f;
    return DataUtil.toInt(timeTamp);
  }

  /**
   * 获取当前时间 Unix截止时间：  new Date().getTime();
   *
   * @return int
   */
  public static Long getCurrentTimeStamp() {
    return getCurrentDate().getTime();
  }

  /**
   * 时间数据  转换成 截止时间
   *
   * @return int
   */
  public static int dateToTimeStamp(Date dateTime) {
    float timeTamp = dateTime.getTime() / 1000f;
    return DataUtil.toInt(timeTamp);
  }

  public static String parseBySecond(int sec) {
    Calendar mCalendar = Calendar.getInstance();
    mCalendar.setTimeInMillis(946656000 * 1000L + (sec * 1000L));
    String value = new SimpleDateFormat(EnumDateFormatter.DATE_TIME_HOUR_STR.getFormatter())
        .format(mCalendar.getTime());
    return value;
  }

  public static String parseHorseByMillSecond(int sec) {
    Calendar mCalendar = Calendar.getInstance();
    mCalendar.setTimeInMillis(946656000 * 1000L + (sec * 1000L));
    String value = new SimpleDateFormat(EnumDateFormatter.DATE_TIME_HOUR_STR.getFormatter())
        .format(mCalendar.getTime());
    return value;
  }

  public static String getTimeByMillSecond(long millSecond) {
    try {
      long time = millSecond / 1000;
      int temp;
      temp = (int) (time / 60);
      return temp + "";
    } catch (Exception e) {
      Log.e("DateHelper", e.toString());
      return "";
    }
  }

  /**
   * 数据转换成dd
   */
  public static short dateToNumDay(Date date) {
    return Short.valueOf(String.valueOf(DateHelper.parseDateNum(date,
        EnumDateFormatter.DATE_DAY_NUM)));
  }

  /**
   * 获取目标时间
   */
  public static Date getTargetCurrentDate(int day) {
    Date date = new Date();
    date.setTime(System.currentTimeMillis() + day * 86400000L);
    return date;
  }

  /**
   * 获取当前时间：HH:mm
   */
  public static String getDateTimeHourMinStr(Date date) {
    if (date == null) date = new Date();
    return new SimpleDateFormat(EnumDateFormatter.DATE_TIME_HOUR_SHORT_STR.getFormatter())
        .format(date);
  }

  /**
   * 根据Data转换成十位时间戳
   */
  public static int getDataUnix(Date date) {
    if (date == null) {
      return 0;
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
    calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
    calendar.set(Calendar.DATE, calendar.get(Calendar.DATE));
    calendar.set(Calendar.HOUR, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    return (int) (calendar.getTime().getTime() / 1000);
  }

  /**
   * 某个月份开始的日期
   */
  public static int getMonthStartDay(int year, int month) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month - 1, 1);
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
    return calendar.get(Calendar.DAY_OF_WEEK) - 1; //0表示 星期日
  }

  /**
   * 获取指定年份指定月份天数
   */
  public static int getIndexMonthDayCount(int year, int month) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month - 1, 1);
    return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  }

  /**
   * 获取0时区当天零点的时间戳
   */
  public static int getTodayZeroZoneDateNum() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return getZeroZoneDateNum((int) (calendar.getTimeInMillis() / 1000));
  }

  /**
   * 获取当天零点的时间戳
   */
  public static int getTodayDateNum() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return (int) (calendar.getTimeInMillis() / 1000);
  }

  /**
   * 获取零时区的时间戳
   */
  public static int getZeroZoneDateNum(int dateNum) {
    return dateNum + getTimeOffset();
  }

  /**
   * 获取当前时区和零时区的偏移
   */
  public static int getTimeOffset() {
    return java.util.TimeZone.getDefault().getRawOffset() / 1000;
  }

  public static int getTimesmorning() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return (int) (cal.getTimeInMillis() / 1000);
  }
}
